import { defineConfig } from 'vite'
import path from 'path'
import fs from 'fs'
import vue from '@vitejs/plugin-vue'

const myPlugin = (limit) => {
  return {
    async transform(_, id) {
      if (process.env.NODE_ENV !== 'development') {
        return
      }
      if (!id.endsWith('.png')) {
        return
      }
      const stat = await fs.promises.stat(id)
      if (stat.size > limit) {
        return
      }
      const buffer = await fs.promises.readFile(id)
      const base64 = buffer.toString('base64')
      const dataUrl = `data:image/png;base64,${base64}`
      return {
        code: `export default '${dataUrl}'`
      }
    }
  }
}

// https://vite.dev/config/
export default defineConfig({
  plugins: [vue(), myPlugin(4096)],
  resolve: {
    // 路径别名
    alias: {
      '@': path.resolve(__dirname, './src')
    }
  },
  build: {
    assetsInlineLimit: 4096
  }
})
